from base import dbObject,dbField
import heapq

class SolarSys(dbObject):
    table='mapSolarSystems'
    id=dbField('solarSystemID')
    name=dbField('solarSystemName')
    security=dbField('security')
    x=dbField('x')
    y=dbField('y')
    z=dbField('z')

    def __init__(self,db,**kw):
        dbObject.__init__(self,db,**kw)
        self.links=[]
        self.db=db

    def calcPMap(self):
        D = {}   # dictionary of final distances
        P = {}   # dictionary of predecessors
        Q = [(0,None,self)]  # heap of (est.dist., pred., vert.)
        while Q:
            dist,pred,v = heapq.heappop(Q)
            if v in D:
                continue  # tuple outdated by decrease-key, ignore
            D[v] = dist
            P[v] = pred
            for w in v.links:
                heapq.heappush(Q, (dist+1, v, w))
        self.DMap, self.PMap = D,P


    def getPath(self,dst):
        if not getattr(self,'PMap',False):
            self.calcPMap()
        path=[dst]
        while path[0] is not self:
                path.insert(0,self.PMap[path[0]])

        assert len(path)==self.DMap[dst]+1
        return path